﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using System.Web;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.POLL.Components;
using ASPNETVN.PORTAL.Components.Share;

namespace ASPNETVN.PORTAL.Modules.POLL
{
    public enum ModulePages
    {
        Send,
        Result        
    }

    public partial class Main : PortalModuleControl
    {
        protected void btPoll_Click(object sender, EventArgs e)
        {
            HttpCookie Voted = Request.Cookies.Get("Voted");
            if (Voted == null)
            {
                int answerID = 0;
                for (int i = 0; i < rdblAnswer.Items.Count; i++)
                {
                    if (rdblAnswer.Items[i].Selected)
                    {
                        answerID = Int32.Parse(rdblAnswer.Items[i].Value);
                        break;
                    }
                }
                Answer.IncreasePoll(answerID);

                Voted = new HttpCookie("Voted");
                Voted.Value = "Yes";
                Voted.Expires = DateTime.Now.AddHours(1.0);
                Response.Cookies.Add(Voted);
            }              
            string url = HttpUrlBuilder.BuildUrl(this.ModuleID, Int32.Parse(ViewState["QuestionID"].ToString()), "Result");
            Response.Redirect(url);            
        }

        protected void btResult_Click(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildUrl(this.ModuleID, Int32.Parse(ViewState["QuestionID"].ToString()), "Result");
            Response.Redirect(url);
        }

        /// <summary>
        /// Load Question and answers
        /// </summary>
        public void LoadData()
        {
            //Set question to lblQuestion lable
            DataSet ds = Answer.SelectBy_ModuleID_Status(this.ModuleID, true);
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                lblQuestion.Text = dt.Rows[0]["Q_Des"].ToString();

                //Set answers correspond with above question
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    rdblAnswer.Items.Add(new ListItem(dt.Rows[i]["A_Des"].ToString(), dt.Rows[i]["A_ID"].ToString()));
                }
                rdblAnswer.Items[0].Selected = true;
                ViewState["QuestionID"] = dt.Rows[0]["Q_ID"].ToString();
            }
            else
            {
                divPoll.Visible = false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ModuleTitle.ManagerUrl = HttpUrlBuilder.BuildAddEditUrl(ModuleID, 0, "Management");
            this.ModuleTitle.AddUrl = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AddEditPoll");
            if (!IsPostBack)
            {
                ViewState["QuestionID"] = 0;
                LoadData();
            }            

        }
    }
}